package com.ziyun.erp.modules.quoteFastPrint.service.impl;

import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.quoteFastPrint.vo.QuoteFastPrintEntityVo;
import com.ziyun.erp.modules.quotePrint.service.QuotePrintService;
import com.ziyun.erp.modules.quotePrintFactoryPrice.entity.QuotePrintFactoryPriceEntity;
import com.ziyun.erp.modules.quotePrintFactoryPrice.service.QuotePrintFactoryPriceService;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.quoteFastPrint.dao.QuoteFastPrintDao;
import com.ziyun.erp.modules.quoteFastPrint.entity.QuoteFastPrintEntity;
import com.ziyun.erp.modules.quoteFastPrint.service.QuoteFastPrintService;


@Service("quoteFastPrintService")
public class QuoteFastPrintServiceImpl extends ServiceImpl<QuoteFastPrintDao, QuoteFastPrintEntity> implements QuoteFastPrintService {
    @Autowired
    private QuoteFastPrintService quoteFastPrintService;

    @Autowired
    private QuotePrintFactoryPriceService quotePrintFactoryPriceService;

    @Autowired
    private QuotePrintService quotePrintService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //只查询快印打印机的
        Page<QuoteFastPrintEntity> page = new Page<>(Integer.parseInt(params.get("page").toString()),Integer.parseInt(params.get("limit").toString()) );
        page.setRecords(this.baseMapper.selectQuoteFastPrintList(page, params));
        return new PageUtils(page);
    }

    @Override
    public void deleteUpdateBatchIds(Integer[] ids, Map<String, Object> map) {
        this.baseMapper.deleteUpdateBatchIds(ids,map);
    }

    @Override
    public List<QuotePrintFactoryPriceEntity> findAllFastPrint(Integer id) {
        return this.baseMapper.findAllFastPrint(id);
    }

    @Override
    public R updateWithPrice(QuoteFastPrintEntityVo quoteFastPrintEntityVo,String createName) {
        ValidatorUtils.validateEntity(quoteFastPrintEntityVo.getQuoteFastPrint());
        //先修改quote_print_factory
        quoteFastPrintService.updateAllColumnById(quoteFastPrintEntityVo.getQuoteFastPrint());
        //取出待修改删除以及新增价格表quote_print_factory_price
        List<QuotePrintFactoryPriceEntity> QuotePrintFactoryPriceList= quoteFastPrintEntityVo.getQuotePrintFacotoryPirceList();
        //待修改集合
        List<QuotePrintFactoryPriceEntity> updateQuotePrintFactoryPriceList= new ArrayList<QuotePrintFactoryPriceEntity>();

        //待新增集合
        List<QuotePrintFactoryPriceEntity> addQuotePrintFactoryPriceList= new ArrayList<QuotePrintFactoryPriceEntity>();
        //取出之前数据库已经存在数据
        List<QuotePrintFactoryPriceEntity> oldQuotePrintFactoryPriceList =quotePrintFactoryPriceService.selectList(new EntityWrapper<QuotePrintFactoryPriceEntity>().eq("print_factory_id",quoteFastPrintEntityVo.getQuoteFastPrint().getId()));
        //待删除集合
        List<QuotePrintFactoryPriceEntity> delQuotePrintFactoryPriceList=new ArrayList<QuotePrintFactoryPriceEntity>();

        //先取出待修改数据，存在id即为待修改,不存在id即为待增加
        for (int i = 0; i < QuotePrintFactoryPriceList.size(); i++) {
            QuotePrintFactoryPriceEntity quotePrintFactoryPriceEntity = QuotePrintFactoryPriceList.get(i);
            if(quotePrintFactoryPriceEntity.getId()!=null){
                updateQuotePrintFactoryPriceList.add(quotePrintFactoryPriceEntity);
            }else{
                //将print_factory_id的值设置为quoteFastPrint.id
                quotePrintFactoryPriceEntity.setPrintFactoryId(quoteFastPrintEntityVo.getQuoteFastPrint().getId());
                //设置创建时间
                quotePrintFactoryPriceEntity.setCreateDate(new Date());
                //设置创建人
                quotePrintFactoryPriceEntity.setCreateUser(createName);
                //设置是否删除
                quotePrintFactoryPriceEntity.setDelFlag(false);
                //设置开机费
                quotePrintFactoryPriceEntity.setBeginPrice(new BigDecimal(0));
                //设置递增价格(以1000印数递增)
                quotePrintFactoryPriceEntity.setStepPrice(new BigDecimal(0));
                //设置是否为特种纸
                quotePrintFactoryPriceEntity.setCanUv(false);
                addQuotePrintFactoryPriceList.add(quotePrintFactoryPriceEntity);
            }
        }
        //先取出原来存储的所有id
        List<Integer> oldIds=oldQuotePrintFactoryPriceList.stream().map(QuotePrintFactoryPriceEntity::getId).collect(Collectors.toList());

        //再取出添加的ids
        List<Integer> updateIds=updateQuotePrintFactoryPriceList.stream().map(QuotePrintFactoryPriceEntity::getId).collect(Collectors.toList());
        oldIds.removeAll(updateIds);
        //待删除ids
        List<Integer> delIds=oldIds;

        //删除
        if(delIds.size()>0){
            Map<String,Object> map=new HashMap<String, Object>();
            map.put("update_user",createName);
            quotePrintFactoryPriceService.deletePriceBatchId(delIds.toArray(new Integer[delIds.size()]),map);
        }
        //添加
        if(addQuotePrintFactoryPriceList.size()>0){
            quotePrintFactoryPriceService.insertBatch(addQuotePrintFactoryPriceList);
        }

        //修改
        if(updateQuotePrintFactoryPriceList.size()>0){
            quotePrintFactoryPriceService.updateBatchById(updateQuotePrintFactoryPriceList);
        }
        return R.ok();
    }
}
